<%@page contentType="text/html; charset=UTF-8"
	trimDirectiveWhitespaces="true" pageEncoding="UTF-8"%>
<html>
<head>
<%@include file="/WEB-INF/include/include.jsp"%>
</head>
<body>
	<div class="layui-fluid">
		<div class="layui-row layui-col-space15">
			<div class="layui-col-md12">
				<div class="layui-card">
					<div class="layui-card-body" pad15>
						<form class="layui-form layui-form-pane"
							action="${pageContext.request.contextPath}/system/user/password"
							method="post">
							<input type="hidden" name="id" value="${model.id }"> <input
								type="hidden" name="corpId" value="${model.corpId }">

							<div class="layui-form-item">
								<label class="layui-form-label">账号</label>
								<div class="layui-input-block">
									<input type="text" value="${model.username}" readonly
										class="layui-input">
								</div>

							</div>

							<div class="layui-form-item">
								<label class="layui-form-label"><font color="red">原始密码</font></label>
								<div class="layui-input-block">
									<input type="password" name="oldPassword" lay-verify="required"  class="layui-input">
								</div>

							</div>

							<div class="layui-form-item">
								<label class="layui-form-label"><font color="red">新密码</font></label>
								<div class="layui-input-block">
									<input type="password" name="newPassword" lay-verify="required|checkpw" class="layui-input">
								</div>

							</div>

							<div class="layui-form-item">
								<label class="layui-form-label"><font color="red">确认新密码</font></label>
								<div class="layui-input-block">
									<input type="password" lay-verify="required|checkpw|confirmpw"  class="layui-input">
								</div>

							</div>
							<div class="layui-form-item">
								<div class="layui-input-block">
									<shiro:hasPermission name="user:edit">
										<button class="layui-btn layui-btn-normal" lay-filter="pageSubmit" lay-submit>提交</button>
										<button type="reset" class="layui-btn layui-btn-primary">重置</button>
									</shiro:hasPermission>
								</div>
							</div>
						</form>
					</div>
				</div>
			</div>
		</div>
	</div>
</body>
<script>
	layui.use([ 'form', 'jquery' ], function() {
		let form = layui.form, $ = layui.jquery;
		
		form.verify({
			checkpw: function(value, item){
				if (!new RegExp("^[a-zA-Z0-9]+$").test(value)){
					return '密码只能包含数字和字母';
				}
			},
			confirmpw: function(value, item){
				let newPassword = $("input[name='newPassword']").val();
				if($.isEmptyObject(value) || value != newPassword){
					return '密码两次输入不一致';
				}
			}
		});
		form.on("submit(pageSubmit)", function(data){
			let $form = $("form");
		    $.ajax({
		    		url: $form.attr("action"),
		    		data: JSON.stringify({
		    			oldPassword: $.trim($form.find("[name='oldPassword']").val()),
		    			newPassword: $form.find("[name='newPassword']").val()
		    		}),
		    		type: 'POST',
		    		dataType: 'json',
		    		contentType : "application/json",
		    		processData: false,
		            success: function(data){
		    	       switch (data.status){
		    	    	   case "incorrect" :
		    	    		   layer.msg("原始密码不正确");
		    	    		   break;
		    	    	   case "login":
		    	    		   $.ajax({url: "${pageContext.request.contextPath}/logout", async: false});
		    	    		   refresh();
		    	    		   break;
		    	       }
		    	   },
		    	   error:function(){
		    		   layer.msg("修改失败");
		    	   }
		    });
		    return false;
		});
	});
</script>
</html>